package finalproject.eda.trees.gui;

import java.awt.Font;
import java.awt.Graphics2D;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import javax.swing.JButton;

import finalproject.eda.trees.logic.PositionControlNodes;
import finalproject.eda.trees.logic.RandomValue;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * @author CassioOliveira
 * @author WildeArruda 
 */
public class MainFrameGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JLabel lblImgAT;
	JPanel mainPanel;
	JPanel treePanel; // Receives the Tree with all nodes
	JTextField valueNode; // Field to put value for insertion or search
	JLabel lblValue;
	JLabel lblNode;
	JButton btnInsert;
	String message;
	Graphics2D g2;
//	Insert bt = new Insert();
	MessageUserGUI messageUserGUI = new MessageUserGUI();
	PositionControlNodes positionControlNodes = new PositionControlNodes(); // Instance of class to positioning nodes on Tree.
	RandomValue randomValue = new RandomValue();

	/**
	 * Create the frame.
	 */
	public MainFrameGUI() {
		setFont(new Font("Batang", Font.PLAIN, 14));
		windowProperties();
		mainPanel();
		menuBar();
		treePanel();
		fieldValueNode();
		insertButton();
		searchButton();
		randomButton();
		resetButton();
		homeButton();
		logoAmbiciousTeam();

		getContentPane().add(mainPanel);
		
		
	}

	/**
	 * Search an node with field value parameter.
	 */
	private void searchButton() {
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				positionControlNodes.searchTree(valueNode.getText());
			}
		});
		btnSearch.setBounds(298, 12, 89, 23);
		mainPanel.add(btnSearch);
	}


	/**
	 * Put the Insert Button on windows and check if Field acceps only numbers.
	 */
	private void insertButton() {
		repaint();
		positionControlNodes.allX();
		final JButton btnInsert = new JButton("Insert");
		btnInsert.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String fieldText = valueNode.getText();// fieldText receives the value typed on textField.
				positionControlNodes.setText(fieldText);
				if(fieldText.isEmpty()){//Checking if Field is empty.
				message = "The value don't be null. Please insert an valid value(Integer).";
					messageUserGUI.infoUser(message);
				}else if(!fieldText.matches("^-?[0-9]{1,}")){//Checking, with regular expressions, if the field is something other than numbers.
					message = "Only accept Integers. Please insert an valid value.";
					messageUserGUI.infoUser(message);
				}else{
					positionControlNodes.position(Integer.parseInt(fieldText), 1);
//					bt.insertNode(Integer.parseInt(fieldText));//Insertion Binary Tree Algorithm called with filed value parameter.
					positionControlNodes.positionNode(positionControlNodes.getRescue(), getWidth());//Setting the position of node.
					newNode(positionControlNodes.getCol(), positionControlNodes.getLin());//Calling the draw method to paint node.
				}
			}
		});
		btnInsert.setBounds(99, 12, 89, 23);
		mainPanel.add(btnInsert);
	}

	private void resetButton() {
		JButton btnReset = new JButton("Reset");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				treePanel.removeAll();
				repaint();
				positionControlNodes.nodes.clear();
				positionControlNodes.allX();
			}
		});
		btnReset.setBounds(811, 11, 89, 23);
		mainPanel.add(btnReset);
	}

	private void randomButton() {
		JButton btnRandom = new JButton("Randomic");
		btnRandom.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				randomValue.RandomVal();
				String randNumber = Integer.toString(randomValue.getRandomic());
				valueNode.setText(randNumber);
			}
		});
		btnRandom.setBounds(188, 12, 110, 23);
		mainPanel.add(btnRandom);
	}
	
	
	private void homeButton(){
		JButton btnReset = new JButton("Home");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				HomeGUI homeGUI = new HomeGUI();
				homeGUI.setVisible(true);
			}
		});
		btnReset.setBounds(900, 11, 89, 23);
		mainPanel.add(btnReset);
	}


	private void fieldValueNode() {
		valueNode = new JTextField();
		valueNode.setBounds(10, 13, 86, 20);
		mainPanel.add(valueNode);
		valueNode.setColumns(10);
		treePanel.setLayout(null);
	}

	private void windowProperties() {
		setBounds(100, 100, 1000, 580);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout(0, 0));
		setResizable(false);
		setLocationRelativeTo(null);
	}

	private void treePanel() {
		treePanel = new JPanel();
		treePanel.setOpaque(false);
		treePanel.setBounds(0, 49, 994, 381);
		mainPanel.add(treePanel);
	}

	private void mainPanel() {
		mainPanel = new JPanel();
		mainPanel.setOpaque(false);
		mainPanel.setLayout(null);
	}

	private void newNode(int x, int y) {
		lblValue = new JLabel(positionControlNodes.getText());
		lblValue.setBounds(x, y, 51, 51);
		treePanel.add(lblValue);
		lblValue.setHorizontalAlignment(SwingConstants.CENTER);
		lblValue.setFont(new Font("Felix Titling", Font.BOLD, 11));

		lblNode = new JLabel("");
		lblNode.setIcon(new ImageIcon(MainFrameGUI.class.getResource("/finalproject/eda/trees/images/Node.png")));
		lblNode.setBounds(x, y, 51, 51);
		treePanel.add(lblNode);
	}
	
	private void menuBar() {
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnTree = new JMenu("File");
		mnTree.setFont(new Font("Batang", Font.PLAIN, 14));
		menuBar.add(mnTree);
		
		JMenu mnNewMenu = new JMenu("New");
		mnNewMenu.setFont(new Font("Batang", Font.PLAIN, 14));
		mnTree.add(mnNewMenu);
		
		JMenuItem mntmNewTree = new JMenuItem("Binary Search Tree");
		mntmNewTree.setFont(new Font("Batang", Font.PLAIN, 14));
		mnNewMenu.add(mntmNewTree);
		{
			JMenuItem mntmBinaryHeap = new JMenuItem("Binary Heap");
			mntmBinaryHeap.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					MainFrameHeapGUI mainFrameHeapGUI = new MainFrameHeapGUI();
					mainFrameHeapGUI.setVisible(true);
				}
			});
			mntmBinaryHeap.setFont(new Font("Batang", Font.PLAIN, 14));
			mnNewMenu.add(mntmBinaryHeap);
		}
		mntmNewTree.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainFrameGUI mainFrameGUI = new MainFrameGUI();
				mainFrameGUI.setVisible(true);
			}
		});
		
		JMenu mnAbout = new JMenu("Help");
		mnAbout.setFont(new Font("Batang", Font.PLAIN, 14));
		menuBar.add(mnAbout);
		
		JMenuItem mntmAbout = new JMenuItem("About");
		mntmAbout.setFont(new Font("Batang", Font.PLAIN, 14));
		mntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AboutGUI aboutGUI = new AboutGUI();
				aboutGUI.setVisible(true);
			}
		});
		mnAbout.add(mntmAbout);
	}


	/**
	 * Insert Group's Logo on window.
	 */
	private void logoAmbiciousTeam() {
		{
			lblImgAT = new JLabel("");
			lblImgAT.setBounds(703, 429, 291, 102);
			mainPanel.add(lblImgAT);
			lblImgAT.setIcon(new ImageIcon(MainFrameGUI.class.getResource("/finalproject/eda/trees/images/ad.png")));
			lblImgAT.setHorizontalAlignment(SwingConstants.CENTER);
			lblImgAT.setHorizontalTextPosition(SwingConstants.CENTER);
		}
	}
}